//-----------------------------------------------------------------------

// <copyright file="DateFieldDefinition.cs" company="Jamie Knight">

// Copyright (c) Jamie Knight. All rights reserved.

// </copyright>

//-----------------------------------------------------------------------

using System;
using System.Windows.Controls;
using System.Windows.Data;

namespace DevKnightlie.WPF.AutoForm.FieldDefinitions
{
    /// <summary>
    /// Field Definition for a date picker field
    /// </summary>
    public class DateFieldDefinition : BaseFieldDefinition
    {
        #region Private Fields

        private DateTime _displayDateStart = new DateTime(1900, 01, 01);
        private DateTime _displayDateEnd = new DateTime(2999, 12, 31);

        #endregion

        #region Properties

        /// <summary>
        /// Sets or returns the start display date of the DatePicker
        /// </summary>
        public DateTime DisplayDateStart
        {
            get
            {
                return _displayDateStart;
            }
            set
            {
                _displayDateStart = value;
            }
        }

        /// <summary>
        /// Sets or returns the end display date of the DatePicker
        /// </summary>
        public DateTime DisplayDateEnd
        {
            get
            {
                return _displayDateEnd;
            }
            set
            {
                _displayDateEnd = value;
            }
        }

        #endregion

        #region Methods

        public override void BuildInputControl()
        {
            DatePicker datePicker = new DatePicker()
            {
                // Provide sane defaults instead of year 0
                DisplayDateStart = new DateTime(1900, 01, 01),
                DisplayDateEnd = new DateTime(2999, 12, 31),
                VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
                HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right,
                Background = System.Windows.Media.Brushes.White
            };
            datePicker.DisplayDateStart = DisplayDateStart;
            datePicker.DisplayDateEnd = DisplayDateEnd;
            Binding dateTimeBinding = new Binding(PropertyName);
            BindingOperations.SetBinding(datePicker, DatePicker.SelectedDateProperty, dateTimeBinding);
            SetupEnabledBinding(datePicker);
            // This fix makes sure the DatePicker background is the correct color - by default the underlying background color shows through
            datePicker.Loaded += delegate
            {
                var textBox1 = (TextBox)datePicker.Template.FindName("PART_TextBox", datePicker);
                if (textBox1 != null)
                {
                textBox1.Background = datePicker.Background;
                }
            };
            ContainerControl = InputControl = datePicker;
        }

        #endregion
    }
}
